package xkp.lesson;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

public class TestJsoup {
    @Test
    public void test1() throws IOException {
       /* String html = "<html><head><title>First parse</title></head>"
                + "<body><p id='id1'>Parsed HTML into a doc.</p></body></html>";
        Document doc = Jsoup.parse(html);
        Element element = doc.getElementById("id1");*/

        //Document代表整个网页
        Document document = Jsoup.connect(
                "http://127.0.0.1:8849/test_jsoup/test_jsoup.html").get();
        //获取网页中的标题
        String title = document.title();
        //获取网页中的p1标签
        Element p1 = document.getElementById("p1");
        //获取标签中的内容
        String p1Content = p1.text();
        //打印标题
        System.out.println(title);
        //打印标签中的内容
        System.out.println(p1Content);

    }

    @Test
    public void test2() throws IOException {
        Document document = Jsoup.connect(
                "http://127.0.0.1:8849/test_jsoup/userinfo.html").get();
        Element table = document.getElementsByTag("table").get(0);
        //通过标签名称
        Element tbody = table.getElementsByTag("tbody").get(0);
        //通过class名称
        Elements allUserNames = tbody.getElementsByClass("userName");
        Elements allUserPwds = tbody.getElementsByClass("userPwd");
        Elements allNickNames = tbody.getElementsByClass("nickName");
        Elements alluserSexs = tbody.getElementsByClass("userSex");
        Elements alluserAges = tbody.getElementsByClass("userAge");
        List<UserInfoEntity> list  =new ArrayList<>();
        for(int i = 0; i< allUserNames.size(); i++){
            String userName = allUserNames.get(i).text();
            String userPwd = allUserPwds.get(i).text();
            String nickName = allNickNames.get(i).text();
            String userSex = alluserSexs.get(i).text();
            Integer userAge = Integer.valueOf(alluserAges.get(i).text());
            UserInfoEntity userInfo = new UserInfoEntity();
            userInfo.setUserName(userName);
            userInfo.setUserPwd(userPwd);
            userInfo.setNickName(nickName);
            userInfo.setUserAge(userAge);
            userInfo.setUserSex(userSex);
            list.add(userInfo);
        }
        list.forEach(System.out::println);
        System.out.println("----------");

    }

    @Test
    public void test3() throws IOException {

        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        //屏蔽日志信息
        LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log",
                "org.apache.commons.logging.impl.NoOpLog");
        java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
        //支持JavaScript
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setActiveXNative(false);
        // webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setTimeout(5000);

        String url="https://www.zhipin.com/c101180100/?query=java&page=2&ka=page-2";
        System.out.println(url);
        HtmlPage rootPage = webClient.getPage(url);
        //设置一个运行JavaScript的时间
        webClient.waitForBackgroundJavaScript(5000);
        String html = rootPage.asXml();

        Document  document =Jsoup.parse(html);
        Elements elementList = document.select(".job-list>ul>li>.job-primary");
        for(Element element:elementList) {
            System.out.println(element.selectFirst(".primary-box").attr("data-jobid"));
            System.out.println(element.selectFirst(".job-name").text());
            System.out.println(element.selectFirst(".job-area").text());
            System.out.println(element.selectFirst(".job-limit>span").text());
            String[] sss = element.selectFirst(".job-limit>p").html().split("<em class=\"vline\"></em>");
            System.out.println(sss[0]);
            System.out.println(sss[1]);
            //System.out.println(element.selectFirst(".job-limit>span").text());
            System.out.println(element.selectFirst(".company-text>.name").text());
            System.out.println("----");
        }


    }
}
